import { isEmpty } from 'lodash-es'

import { getCurrentUser } from '@/api/auth'
import { useEventBus } from '@/event-bus'
import { useUserStore, toRefsUserStore } from '@/stores'

import type { Router } from 'vue-router'

const Layout = () => import('@/layout/index.vue')

export function setupRouterGuard(router: Router) {
  const { resolveMenuRoute, cleanup } = useUserStore()

const { token, user } = toRefsUserStore()
  const { routerEventBus } = useEventBus()

  router.beforeEach(async (to, from, next) => {
    routerEventBus.emit('beforeEach')
    if (to.name === 'signIn') {
      if (!token.value) {
        next()
      } else {
        next(from.fullPath)
      }

      return false
    }
    if (!token.value) {
      // 如果未登录且访问的是首页，重定向到登录页
      if (to.path === '/') {
        next({ name: 'signIn' })
      } else {
        cleanup()
        next()
      }
      return false
    }

    if (token.value && !router.hasRoute('layout')) {
      try {
        // 确保用户信息是最新的，特别是权限列表
        if (!user.value?.permissions || user.value.permissions.length === 0) {
          const currentUserInfo = await getCurrentUser()
          user.value = currentUserInfo
        }

        const { routeList } = await resolveMenuRoute()

        if (isEmpty(routeList)) {
          cleanup()
          next()
          return false
        }

        router.addRoute({
          path: '/',
          name: 'layout',
          component: Layout,
          // if you need to have a redirect when accessing / routing
          redirect: '/dashboard',
          children: routeList,
        })

        next(to.fullPath)
      } catch (error) {
        console.error('Error resolving user menu or adding route:', error)
        cleanup()
        next({ name: 'signIn' })
      }

      return false
    }

    next()
    return false
  })

  router.beforeResolve((_, __, next) => {
    next()
  })

  router.afterEach(() => {
    routerEventBus.emit('afterEach')
  })
}
